package com.neuedu.javase.api;

import java.io.UnsupportedEncodingException;

/**
 * @auth 金山老师
 * title:   书籍代码
 * creatime: 2022-09-28 13:59
 * 描述 :
 */
public class MyString {

    public static void main(String[] args) throws UnsupportedEncodingException {

        //java.lang 不需要导包
//        创建对象
        String str1 = new String("abc");

        char chrs[] = {'d' ,'e','f'};
        String str2 = new String(chrs);

        String str3= new String(chrs,1,2);

        String strUtf8 = "国庆节";
        byte bytes[] =  strUtf8.getBytes("GBK");
        String str4 = new String(bytes,"GBK");


        //常用的字符串创建
        String str5 =  "字面量的赋值";



        System.out.println("str1 = " + str1);
        System.out.println("str2 = " + str2);
        System.out.println("str3 = " + str3);
        System.out.println("str4 = " + str4);
        System.out.println("str5 = " + str5);

        System.out.println("===================常用的方法========================");

        String myStr1 =  "abcdef";
        String myStr2 =  new String("abcdef");
        String myStr3 =  new String("abcdef");
        String myStr4 =  "abcdef";
        String myStr5 =  "abc"+"def";   //编译器会进行优化      String myStr5 =  "abcdef";

        //判断字符串字面量是否相等
        System.out.println("myStr1 == myStr1 = " + (myStr1 == myStr2));  //false
        System.out.println("myStr2 == myStr3 = " + (myStr2 == myStr3));  //false


        System.out.println("myStr1.equals(myStr2) = " + myStr1.equals(myStr2));  //true
        System.out.println("myStr2.equals(myStr3) = " + myStr2.equals(myStr3));  //true
        System.out.println("myStr1 == myStr4 = " + (myStr1 == myStr4));
        System.out.println("myStr4 == myStr5 = " + (myStr4 == myStr5));

        Integer inta = 500;
        Integer intb = 500;
        System.out.println("inta == intb = " + (inta == intb));  //false


        //  -128   ~ 127
        Integer intc = 127;
        Integer intd = 127;
        System.out.println("intc == intd = " +  ( intc == intd));  //true

        Integer inte = 128;
        Integer intf = 128;
        System.out.println("inte == intf = " +  ( inte == intf));  //false


        /**
         * | boolean equals(String)              | 判断两个字符串对象的内容是否相等                             |
         * | ----------------------------------- | ------------------------------------------------------------ |
         * | boolean equalsIgnoreCase(String)    | 比较两个字符串的内容是否相等，忽略大小写                     |
         * | String toUpperCase( )               | 将String对象中的所有字符都转换为大写                         |
         * | String toLowerCase( )               | 将String对象中的所有字符都转换为小写                         |
         * | char charAt(int)                    | 返回指定索引处的 char 值                                     |
         * | String substring(int begin)         | 返回一个新字符串，该字符串是从begin开始的字符串的内容        |
         * | String substring(int begin,int end) | 返回一个新字符串，该字符串是从begin开始到end-1结束的字符串的内容 |
         *
         *
         *
         */

        String mStr1 = "abc";
        String mStr2 = "Abc";
        System.out.println("mStr1.equals(mStr2) = " + mStr1.equals(mStr2));
        System.out.println("mStr1.equalsIgnoreCase(mStr2) = " + mStr1.equalsIgnoreCase(mStr2));

        String mStr3 = "AbcDefAB";
        //返回新的字符串
        System.out.println("mStr3.toUpperCase() = " + mStr3.toUpperCase());
        System.out.println("mStr3.toLowerCase() = " + mStr3.toLowerCase());
        System.out.println("mStr3 = " + mStr3);

        System.out.println("mStr3.charAt(0) = " + mStr3.charAt(0));
        System.out.println("mStr3.charAt(1) = " + mStr3.charAt(1));
        //length() 获取字符串长度
        for (int i = 0; i < mStr3.length(); i++) {
            System.out.println("mStr3.charAt("+i+") = " + mStr3.charAt(i));
        }


        //length()   6
        System.out.println("mStr3.substring(3) = " + mStr3.substring(3));
        System.out.println("mStr3.substring(3,5) = " + mStr3.substring(3,5));  // [start:3  ,end:5  )

        /*
         *
         * | **方法**                             | **含义**                                                     |
         * | ------------------------------------ | ------------------------------------------------------------ |
         * | int  indexOf/lastIndexOf(char)       | 返回指定字符在此字符串中第一次/最后一次出现处的索引。        |
         * | int  indexOf/lastIndexOf(char,int)   | 从指定的索引开始搜索，返回在此字符串中第一次/最后一次出现指定字符处的索引 |
         * | int  indexOf/lastIndexOf(String)     | 返回第一次出现的指定子字符串在此字符串中的索引               |
         * | int  indexOf/lastIndexOf(String,int) | 从指定的索引开始搜索，返回在此字符串中第一次/最后一次出现指定字符串处的索引 |
         * | String trim( )                       | 返回新的字符串，忽略前导空白和尾部空白                       |
         * | int  length( )                       | 返回此字符串的长度                                           |
         */
        //              H e l l o   W  o  r  l  d
        //              0 1 2 3 4 5 6  7  8  9  10
        String mStr4 = "Hello World";
        System.out.println("mStr4 = " + mStr4);
        System.out.println("mStr4.indexOf(\"l\") = " + mStr4.indexOf("l"));  //2
        System.out.println("mStr4.indexOf(\"l\") = " + mStr4.indexOf("l",3));  //3
        System.out.println("mStr4.lastIndexOf(\"l\") = " + mStr4.lastIndexOf("l"));  //9


        String fileName ="112121211201ddab81-52ee-425a-922d-8aa87002db07.pdf";

        String ext = fileName.substring(fileName.lastIndexOf("."));
        System.out.println("fileName = " + fileName);
        System.out.println("ext = " + ext);

        String  trimOldStr = " abc ";
        System.out.println("trimOldStr = " + trimOldStr);
        System.out.println("trimOldStr.length() = " + trimOldStr.length());
        String trimNewStr = trimOldStr.trim();  //产生新字符串
        System.out.println("trimNewStr.trim() = " + trimNewStr);
        System.out.println("trimNewStr.trim() = " + trimNewStr.length());
        System.out.println("trimOldStr = " + trimOldStr);
        System.out.println("trimOldStr.length() = " + trimOldStr.length());


        /* | **方法**                                   | **含义**                                                     |
         * | ------------------------------------------ | ------------------------------------------------------------ |
         * | String  concat(String str)                 | 将指定字符串连接到此字符串的结尾                             |
         * | byte[]  getBytes()                         | 使用平台的默认字符集将此 String 编码为 byte 序列，并将结果存储到一个新的 byte 数组中 |
         * | byte[] getBytes(Charset charset)           | 使用给定的 charset将此 String 编码到 byte 序列，并将结果存储到新的 byte 数组 |
         * | String[] split(String regex)               | 根据给定正则表达式的匹配拆分此字符串。                       |
         * | String replace(char oldChar, char newChar) | 返回一个新的字符串，它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的 |
         * | boolean startsWith(String prefix)          | 测试此字符串是否以指定的前缀开始                             |
         * | boolean  endsWith(String suffix)           | 测试此字符串是否以指定的后缀结束                             |
         */

        String concat1 = "abc";
        String concat2 = "def";

        //产生的是新字符胡灿
        System.out.println("concat1.concat(concat2) = " + concat1.concat(concat2));
        System.out.println("concat1 = " + concat1);
        System.out.println("concat2= " + concat2);


        String splitStr = "abc-def-gggg-sdsaf";
        String[] splitArr = splitStr.split("-");
        for (String s : splitArr) {
            System.out.println("s = " + s);
        }


        String hello = "Hello  My name is jshand";

        System.out.println("hello = " + hello);
        //产生新字符串
        System.out.println("hello.replace(\"n\",\"N\") = " + hello.replace("n", "N"));
//        System.out.println("hello.replace(\"n\",\"N\") = " + hello.replaceFirst("n", "N"));
        System.out.println("hello = " + hello);

        System.out.println("hello.startsWith(\"Hello\") = " + hello.startsWith("Hello"));  //true
        System.out.println("hello.endWith(\"d\") = " + hello.endsWith("d"));  //true
        System.out.println("hello.endWith(\"e\") = " + hello.endsWith("e"));  //false

        System.out.println("hello.contains(\"name\") = " + hello.contains("name")); //true


        System.out.println("hello.indexOf(\"name\") = " + hello.indexOf("name"));  //>-1
        System.out.println("hello.indexOf(\"abc\") = " + hello.indexOf("abc"));  //  -1


    }
}
